﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using UCenter.Core.Model;

namespace UCenter.Models
{
    [Table(Name = "dbo.UserModuleMap")]
    public class UserModuleMap : EntityBase
    {
        #region Private Fields

        private int _Id;

        private int _userId;

        private int _moduleId;

        private DateTime _addDate;

        private int _rank;

        private EntityRef<Module> _module;

        private EntityRef<User> _user;

        #endregion

        #region Constructor

        public UserModuleMap()
        {
            this._module = default(EntityRef<Module>);
            this._user = default(EntityRef<User>);
        }

        #endregion

        #region Public Propeties

        [Column(Name = "ID", Storage = "_Id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public int ID
        {
            get
            {
                return this._Id;
            }
            set
            {
                if (this._Id != value)
                {
                    this.SendPropertyChanging("ID");
                    this._Id = value;
                    this.SendPropertyChanged("ID", value);
                }
            }
        }

        [Column(Name = "UserId", Storage = "_userId", DbType = "Int NOT NULL")]
        public int UserId
        {
            get
            {
                return this._userId;
            }
            set
            {
                if (this._userId != value)
                {
                    if (this._user.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }

                    this.SendPropertyChanging("UserId");
                    this._userId = value;
                    this.SendPropertyChanged("UserId", value);
                }
            }
        }

        [Column(Name = "ModuleId", Storage = "_moduleId", DbType = "Int NOT NULL")]
        public int ModuleId
        {
            get
            {
                return this._moduleId;
            }
            set
            {
                if (this._moduleId != value)
                {
                    if (this._module.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }

                    this.SendPropertyChanging("ModuleId");
                    this._moduleId = value;
                    this.SendPropertyChanged("ModuleId", value);
                }
            }
        }

        [Column(Name = "AddDate", Storage = "_addDate", DbType = "DateTime NOT NULL")]
        public DateTime AddDate
        {
            get
            {
                return this._addDate;
            }
            set
            {
                if (this._addDate != value)
                {
                    this.SendPropertyChanging("AddDate");
                    this._addDate = value;
                    this.SendPropertyChanged("AddDate", value);
                }
            }
        }

        [Column(Name = "Rank", Storage = "_rank", DbType = "Int NOT NULL")]
        public int Rank
        {
            get
            {
                return this._rank;
            }
            set
            {
                if (this._rank != value)
                {
                    this.SendPropertyChanging("Rank");
                    this._rank = value;
                    this.SendPropertyChanged("Rank", value);
                }
            }
        }

        [Association(Name = "Module_UserModuleMap", Storage = "_module", ThisKey = "ModuleId", IsForeignKey = true)]
        public Module Module
        {
            get
            {
                return this._module.Entity;
            }
            set
            {
                Module previousValue = this._module.Entity;
                if (((previousValue != value)
                            || (this._module.HasLoadedOrAssignedValue == false)))
                {
                    if ((previousValue != null))
                    {
                        this._module.Entity = null;
                        previousValue.UserModuleMaps.Remove(this);
                    }
                    this._module.Entity = value;
                    if ((value != null))
                    {
                        value.UserModuleMaps.Add(this);
                        this._moduleId = value.ID;
                    }
                    else
                    {
                        this._moduleId = default(int);
                    }
                }
            }
        }

        [Association(Name = "User_UserModuleMap", Storage = "_user", ThisKey = "UserId", IsForeignKey = true)]
        public User User
        {
            get
            {
                return this._user.Entity;
            }
            set
            {
                User previousValue = this._user.Entity;
                if (((previousValue != value)
                            || (this._user.HasLoadedOrAssignedValue == false)))
                {
                    if ((previousValue != null))
                    {
                        this._user.Entity = null;
                        previousValue.UserModuleMaps.Remove(this);
                    }
                    this._user.Entity = value;
                    if ((value != null))
                    {
                        value.UserModuleMaps.Add(this);
                        this._userId = value.ID;
                    }
                    else
                    {
                        this._userId = default(int);
                    }
                }
            }
        }

        #endregion
    }
}
